package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.provider.dao.OldmanFeeMapper;
import com.csun.cmny.provider.dao.OldmanRechargeMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcOldManAccountFunds;
import com.csun.cmny.provider.pojo.OldmanFee;
import com.csun.cmny.provider.pojo.OldmanRecharge;
import com.csun.cmny.provider.service.OldmanRechargeService;
import com.csun.cmny.provider.service.OmcOldManAccountFundsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class OldmanRechargeServiceImpl implements OldmanRechargeService {
    @Resource
    private OldmanRechargeMapper oldmanRechargeMapper;
    @Resource
    private OmcOldManAccountFundsService omcOldManAccountFundsService;

    @Resource
    private OldmanFeeMapper oldmanFeeMapper;
    @Override
    public int deleteByPrimaryKey(Integer rechargeId) {
        return 0;
    }
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public int insert(OldmanRecharge oldmanRecharge) {
        OldmanFee oldmanFee =  oldmanFeeMapper.selectByOldmanId(oldmanRecharge.getOldmanId());
        BigDecimal remainder = oldmanFee.getRemainder();//充值之前的余额
        BigDecimal AfterRemainder = remainder.add(oldmanRecharge.getRechargeMoney());
        oldmanRecharge.setRemainder(AfterRemainder);
        int n = oldmanRechargeMapper.insertSelective(oldmanRecharge);
        int m = 0;
        if (n > 0){//如果充值成功，更改消费单余额
            OldmanFee oldmanFeeAfter = new OldmanFee();
            oldmanFeeAfter.setCheckinoldmanId(oldmanRecharge.getOldmanId());
            oldmanFeeAfter.setRemainder(AfterRemainder);
             m = oldmanFeeMapper.updateByPrimaryKeySelective(oldmanFeeAfter);
            if (m <1 ){
                System.out.println("********************充值成功后消费单余额修改失败********************");
            }

        }else {
            System.out.println("********************充值失败********************");

        }
        if (n>0 && m>0){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public int insertSelective(OldmanRecharge record) {
        return oldmanRechargeMapper.insert(record);
    }

    @Override
    public OldmanRecharge selectByPrimaryKey(Integer rechargeId) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(OldmanRecharge record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(OldmanRecharge record) {
        return 0;
    }

    @Override
    public List<OldmanRecharge> showRechargeAll(Map<String,Object> queryMap) {
        return oldmanRechargeMapper.showRechargeAll(queryMap);
    }

    @Override
    public int deleteByOldmanId(Integer oldmanId) {
        return oldmanRechargeMapper.deleteByOldmanId(oldmanId);
    }

    @Override
    public BigDecimal getRechargeTotal(Integer oldmanId) {
        return oldmanRechargeMapper.getRechargeTotal(oldmanId);
    }

    @Override
    public int recharge(OldmanRecharge oldmanRecharge, String uuid) {
        if (oldmanRecharge.getUuid().equals(uuid)){
            //修改老人账户金额
            OmcOldManAccountFunds accountFundsQuery = new OmcOldManAccountFunds();
            accountFundsQuery.setOldManId(oldmanRecharge.getOldmanId().longValue());
            accountFundsQuery = omcOldManAccountFundsService.selectOne(accountFundsQuery);
            if(accountFundsQuery != null){
                accountFundsQuery.setRemainder(accountFundsQuery.getRemainder().add(oldmanRecharge.getRechargeMoney()));
                if(accountFundsQuery.getRecharge() == null){
                    accountFundsQuery.setRecharge(BigDecimal.ZERO);
                }
                accountFundsQuery.setRecharge(accountFundsQuery.getRecharge().add(oldmanRecharge.getRechargeMoney()));
                int ret = omcOldManAccountFundsService.update(accountFundsQuery);
            }
            oldmanRecharge.setRemainder(accountFundsQuery.getRemainder());
            int n = oldmanRechargeMapper.insert(oldmanRecharge);
            redisTemplate.delete("randomuuid");
            if (n>0){
                return n;
            }else{
                throw new OmcBizException(ErrorCodeEnum.OMC10037047);
            }
        }else {
            throw new OmcBizException(ErrorCodeEnum.OMC10037046);
        }
    }
}
